package dobest.ray.com.jiongjiong.views;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;

public class CanAdapterImageView extends ImageView {

    private Context context;

    public CanAdapterImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;
    }

    public CanAdapterImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public CanAdapterImageView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        if (drawable != null) {

            setHeightAndWidth(((BitmapDrawable) drawable).getBitmap().getWidth(),
                    ((BitmapDrawable) drawable).getBitmap().getHeight());
        }
        super.setImageDrawable(drawable);
    }

    private void setHeightAndWidth(int width, int height) {
        WindowManager wm = ((Activity) context).getWindowManager();
        int screenWidth = wm.getDefaultDisplay().getWidth();
        LayoutParams params = getLayoutParams();
        screenWidth -= 40;
        params.height = height * screenWidth / width;
        params.width = screenWidth;
        setLayoutParams(params);
    }

    @Override
    public void setImageResource(int resId) {
        super.setImageResource(resId);
    }

    @Override
    public void setImageURI(Uri uri) {
        super.setImageURI(uri);
    }

}
